1   /*
2    * Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package java.awt.image;
27  
28  
29  /**
30   * This class defines a lookup table object.  The output of a
31   * lookup operation using an object of this class is interpreted
32   * as an unsigned short quantity.  The lookup table contains short
33   * data arrays for one or more bands (or components) of an image,
34   * and it contains an offset which will be subtracted from the
35   * input values before indexing the arrays.  This allows an array
36   * smaller than the native data size to be provided for a
37   * constrained input.  If there is only one array in the lookup
38   * table, it will be applied to all bands.
39   *
40   * @see ByteLookupTable
41   * @see LookupOp
42   */
43  public class ShortLookupTable extends LookupTable {
44  
45      /**
46       * Constants
47       */
48  
49      short data[][];
50  
51      /**
52       * Constructs a ShortLookupTable object from an array of short
53       * arrays representing a lookup table for each
54       * band.  The offset will be subtracted from the input
55       * values before indexing into the arrays.  The number of
56       * bands is the length of the data argument.  The
57       * data array for each band is stored as a reference.
58       * @param offset the value subtracted from the input values
59       *        before indexing into the arrays
60       * @param data an array of short arrays representing a lookup
61       *        table for each band
62       */
63      public ShortLookupTable(int offset, short data[][]) {
64          super(offset,data.length);
65          numComponents = data.length;
66          numEntries    = data[0].length;
67          this.data = new short[numComponents][];
68          // Allocate the array and copy the data reference
69          for (int i=0; i < numComponents; i++) {
70              this.data[i] = data[i];
71          }
72      }
73  
74      /**
75       * Constructs a ShortLookupTable object from an array
76       * of shorts representing a lookup table for each
77       * band.  The offset will be subtracted from the input
78       * values before indexing into the array.  The
79       * data array is stored as a reference.
80       * @param offset the value subtracted from the input values
81       *        before indexing into the arrays
82       * @param data an array of shorts
83       */
84      public ShortLookupTable(int offset, short data[]) {
85          super(offset,data.length);
86          numComponents = 1;
87          numEntries    = data.length;
88          this.data     = new short[1][];
89          this.data[0]  = data;
90      }
91  
92      /**
93       * Returns the lookup table data by reference.  If this ShortLookupTable
94       * was constructed using a single short array, the length of the returned
95       * array is one.
96       * @return ShortLookupTable data array.
97       */
98      public final short[][] getTable(){
99          return data;
100     }
101 
102     /**
103      * Returns an array of samples of a pixel, translated with the lookup
104      * table. The source and destination array can be the same array.
105      * Array <code>dst</code> is returned.
106      *
107      * @param src the source array.
108      * @param dst the destination array. This array must be at least as
109      *         long as <code>src</code>.  If <code>dst</code> is
110      *         <code>null</code>, a new array will be allocated having the
111      *         same length as <code>src</code>.
112      * @return the array <code>dst</code>, an <code>int</code> array of
113      *         samples.
114      * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
115      *            longer than <code>dst</code> or if for any element
116      *            <code>i</code> of <code>src</code>,
117      *            <code>(src[i]&0xffff)-offset</code> is either less than
118      *            zero or greater than or equal to the length of the
119      *            lookup table for any band.
120      */
121     public int[] lookupPixel(int[] src, int[] dst){
122         if (dst == null) {
123             // Need to alloc a new destination array
124             dst = new int[src.length];
125         }
126 
127         if (numComponents == 1) {
128             // Apply one LUT to all channels
129             for (int i=0; i < src.length; i++) {
130                 int s = (src[i]&0xffff) - offset;
131                 if (s < 0) {
132                     throw new ArrayIndexOutOfBoundsException("src["+i+
133                                                              "]-offset is "+
134                                                              "less than zero");
135                 }
136                 dst[i] = (int) data[0][s];
137             }
138         }
139         else {
140             for (int i=0; i < src.length; i++) {
141                 int s = (src[i]&0xffff) - offset;
142                 if (s < 0) {
143                     throw new ArrayIndexOutOfBoundsException("src["+i+
144                                                              "]-offset is "+
145                                                              "less than zero");
146                 }
147                 dst[i] = (int) data[i][s];
148             }
149         }
150         return dst;
151     }
152 
153     /**
154      * Returns an array of samples of a pixel, translated with the lookup
155      * table. The source and destination array can be the same array.
156      * Array <code>dst</code> is returned.
157      *
158      * @param src the source array.
159      * @param dst the destination array. This array must be at least as
160      *         long as <code>src</code>.  If <code>dst</code> is
161      *         <code>null</code>, a new array will be allocated having the
162      *         same length as <code>src</code>.
163      * @return the array <code>dst</code>, an <code>int</code> array of
164      *         samples.
165      * @exception ArrayIndexOutOfBoundsException if <code>src</code> is
166      *            longer than <code>dst</code> or if for any element
167      *            <code>i</code> of <code>src</code>,
168      *            <code>(src[i]&0xffff)-offset</code> is either less than
169      *            zero or greater than or equal to the length of the
170      *            lookup table for any band.
171      */
172     public short[] lookupPixel(short[] src, short[] dst){
173         if (dst == null) {
174             // Need to alloc a new destination array
175             dst = new short[src.length];
176         }
177 
178         if (numComponents == 1) {
179             // Apply one LUT to all channels
180             for (int i=0; i < src.length; i++) {
181                 int s = (src[i]&0xffff) - offset;
182                 if (s < 0) {
183                     throw new ArrayIndexOutOfBoundsException("src["+i+
184                                                              "]-offset is "+
185                                                              "less than zero");
186                 }
187                 dst[i] = data[0][s];
188             }
189         }
190         else {
191             for (int i=0; i < src.length; i++) {
192                 int s = (src[i]&0xffff) - offset;
193                 if (s < 0) {
194                     throw new ArrayIndexOutOfBoundsException("src["+i+
195                                                              "]-offset is "+
196                                                              "less than zero");
197                 }
198                 dst[i] = data[i][s];
199             }
200         }
201         return dst;
202     }
203 
204 }